package com.bosicen.crm.dao.sysmanage;
import java.util.List;


import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import com.bosicen.crm.dao.HibernateDao;
import com.bosicen.crm.dao.entity.Dealer;
import com.bosicen.crm.tools.Page;
/**
 * 经销商信息
 * @author tigerlei
 *
 */
@Component("dealerDao")
@SuppressWarnings("unchecked")
public class DealerDao extends HibernateDao<Dealer, Integer> {

	/****
	 * 查询列表
	 * @param id
	 * @return
	 */
	
	public List<Dealer> findDealerList(int companyid){
		return this.getSession().createQuery(" from Dealer where companyid=?  order by id asc").setParameter(0, companyid).list();
	}
	
	/****
	 * 查询列表
	 * @param id
	 * @return
	 */
	
	public List<Dealer> findDealerList(){
		return this.getSession().createQuery(" from Dealer  order by id desc").list();
	}
	
	/**
	 * 按ID查询
	 * @param id
	 * @return
	 */
	public Dealer findDealerById(int id){
		return this.get(id);
	}
	
	/***
	 * 删除
	 * @param c
	 */
	public void delDealer(Dealer c){
		this.delete(c.getId());
	}
	
	/**
	 * 保存
	 */
	public void saveOrUpdate(Dealer company){
		this.getSession().saveOrUpdate(company);
	}
	
	/****
	 * 查询列表
	 * @param id
	 * @return
	 */
	
	public Page findDealer4Page(Dealer dealer,Page page){
		String sql = " from Dealer where companyid=? ";
		if(dealer != null){
			if(StringUtils.isNotBlank(dealer.getName())){
				sql += " and name like '%"+dealer.getName()+"%' ";
			}
			if(StringUtils.isNotBlank(dealer.getAddress())){
				sql += " and address like '%"+ dealer.getAddress()+"%' ";
			}
			if(dealer.getCityid()>0 && dealer.getCityid() != 86){
				sql += " and cityid="+dealer.getCityid();
			}
		}
		sql += " order by id desc ";
		return this.findPage(page,sql,dealer.getCompanyid());
	}
}
